package cn.com.headfree.point.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TPointRuleInfo;
import cn.com.headfree.entity.TRuleElementInfo;
import cn.com.headfree.entity.TRuleProjectInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.point.service.PointRuleService;
import cn.com.headfree.point.service.RuleElementService;
import cn.com.headfree.point.service.RuleProjectService;
import cn.com.headfree.util.SysDictionaryUtil;
import cn.com.headfree.vo.SysDictionaryInfoVo;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 *积分配置
 */
@Controller
@RequestMapping(value = "/point", produces = "application/json;charset=UTF-8")
public class PointController extends ExBaseController {
	private static final Logger LOG = LogManager.getLogger(PointController.class);

	@Autowired
	private PointRuleService pointRuleService;

	@Autowired
	private RuleProjectService ruleProjectService;

	@Autowired
	private RuleElementService ruleElementService;

	/**
	 * 保存
	 */
	@RequestMapping("/savePointInfo")
	@ResponseBody
	public ResultData savePointInfo(HttpServletRequest request, TPointRuleInfo ruleInfo){
		ResultData result = new ResultData();
		try{
			User user = this.getLoginUserInfo(request);
			ruleInfo.setCreaterId(user.getFax());
			ruleInfo.setCreaterName(user.getName());
			ruleInfo.setCreateTime(new Date());
			ruleInfo.setUpdaterId(user.getFax());
			ruleInfo.setUpdaterName(user.getName());
			ruleInfo.setUpdateTime(new Date());
			ruleInfo.setState(0);//默认未启用
			ruleInfo.setDelSign(0);
			ruleInfo.setOrgId(user.getBaseOrgId());
			ruleInfo.setOrgName(user.getBaseOrgName());
			pointRuleService.savePointRuleInfo(ruleInfo);
			result.setSuccess(true);
			result.setData("保存成功");
			LOG.info("保存积分配置规则成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("保存失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 根据组织，分页查询
	 */
	@RequestMapping("/getPagePointRuleInfo")
	@ResponseBody
	public ResultData getPagePointRuleInfo(HttpServletRequest request,Page<TPointRuleInfo> page,TPointRuleInfo pointRuleInfo){
		ResultData result = new ResultData();
		try{
			User user = this.getLoginUserInfo(request);
			pointRuleService.getPagePointRuleInfo(page,pointRuleInfo,user);
			result.setSuccess(true);
			result.setData(page);
			LOG.info("查询积分配置规则成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("查询失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 规则启用
	 */
	@RequestMapping("/useRuleInfo")
	@ResponseBody
	public ResultData useRuleInfo(HttpServletRequest request,String ruleId){
		ResultData result = new ResultData();
		LOG.info("===========参数ruleId为========="+ruleId);
		if(StringUtil.isNullOrEmpty(ruleId) || ruleId.length() > 50){
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}
		try{
			User user = this.getLoginUserInfo(request);
			pointRuleService.useRuleInfo(ruleId,user);
			result.setSuccess(true);
			result.setData("操作成功");
			LOG.info("规则启用操作成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("操作失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 一键复用
	 */
	@RequestMapping("oneKeyCopyInfo")
	@ResponseBody
	public ResultData oneKeyCopyInfo(HttpServletRequest request,TPointRuleInfo pointRuleInfo){
		ResultData result = new ResultData();
		if(StringUtil.isNullOrEmpty(pointRuleInfo.getId())){
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}
		try{
			User user = this.getLoginUserInfo(request);
			pointRuleService.oneKeyCopyInfo(pointRuleInfo,user);
			result.setSuccess(true);
			result.setData("复用成功");
			LOG.info("一键复用操作成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("复用失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 配置项展示
	 */
	@RequestMapping("/findProjectInfoListByPid")
	@ResponseBody
	public ResultData findProjectInfoListByPid(HttpServletRequest request,String id){
		ResultData result = new ResultData();
		LOG.info("============参数id值=========="+id);
		if(StringUtil.isNullOrEmpty(id)){
			result.setErrorMsg("参数错误");
			result.setSuccess(false);
			return result;
		}
		try{
			User user = this.getLoginUserInfo(request);
			List<TRuleProjectInfo> list = ruleProjectService.findProjectInfoListByPid(id,user.getBaseOrgId());
			result.setSuccess(true);
			result.setData(list);
			LOG.info("查询配置项成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("获取配置项失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 加分项展示
	 */
	@RequestMapping("findElementInfoList")
	@ResponseBody
	public ResultData findElementInfoList(HttpServletRequest request,Page<TRuleElementInfo> page,TRuleElementInfo ruleElementInfo){
		ResultData result = new ResultData();
		if(StringUtil.isNullOrEmpty(ruleElementInfo.getProjectId())){
			result.setSuccess(false);
			result.setErrorMsg("参数错误");
			return result;
		}
		try{
			User user = this.getLoginUserInfo(request);
			ruleElementInfo.setOrgId(user.getBaseOrgId());
			ruleElementInfo.setOrgName(user.getBaseOrgName());
			this.ruleElementService.findRuleElementPageListByProjectId(page,ruleElementInfo);
			result.setSuccess(true);
			result.setData(page);
			LOG.info("查询加分项成功");
			LOG.info("查询加分项列表成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("查询失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 加分项保存
	 */
	@RequestMapping("saveElementInfo")
	@ResponseBody
	public ResultData saveElementInfo(HttpServletRequest request, TRuleElementInfo ruleElementInfo){
		ResultData result = new ResultData();
		try{
			User user = this.getLoginUserInfo(request);
			this.ruleElementService.saveElementInfo(ruleElementInfo,user);
			result.setSuccess(true);
			result.setData("保存成功");
			LOG.info("保存加分项成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("保存失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 加分项删除
	 */
	@RequestMapping("deleteElementInfo")
	@ResponseBody
	public ResultData deleteElementInfo(HttpServletRequest request, String id){
		ResultData result = new ResultData();
		try{
			User user = this.getLoginUserInfo(request);
			TRuleElementInfo ruleElementInfo = new TRuleElementInfo();
			ruleElementInfo.setId(id);
			ruleElementInfo.setDelSign(1);
			ruleElementInfo.setUpdaterId(user.getFax());
			ruleElementInfo.setUpdaterName(user.getName());
			ruleElementInfo.setUpdateTime(new Date());
			this.ruleElementService.deleteElementInfo(ruleElementInfo);
			result.setSuccess(true);
			result.setData("删除成功");
			LOG.info("删除加分项成功");
		}catch (Exception e){
			result.setSuccess(false);
			result.setErrorMsg("删除失败");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 获取字典信息
	 * @param
	 * @return
	 */
	@RequestMapping("/getSysDictionaryByValue")
	@ResponseBody
	public ResultData getSysDictionaryByValue(String value) {
		ResultData result = new ResultData();
		try {
			List<SysDictionaryInfoVo> sysDictionaryInfoVos = SysDictionaryUtil.getSysDictionaryInfoListByValue(value);
			result.setData(sysDictionaryInfoVos);
		} catch (Exception e)
		{
			result.setErrorMsg("数据字典获取成功！");
			result.setSuccess(false);
			LOG.error("数据字典获取异常", e);
		}
		return result;
	}
}
